﻿using AutoMapper;
using Hx.ADSyncPlatform.Common.Log;
using Hx.ADSyncPlatform.User;
using Hx.ADSyncPlatform.WebApi.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;

namespace Hx.ADSyncPlatform.Controllers.Base
{
    [Route("api/[controller]")]
    [ApiController]
    public class BaseController : Controller
    {
        public IMapper mapper;
        public readonly PlatManager platManager;
        public IHttpContextAccessor httpContextAccessor;
        public readonly LogHelper logHelper;
        protected string dataPermissionCacheKey = "{0}-DATAPERMISSION";

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceProvider"></param>
        public BaseController(IServiceProvider serviceProvider)
        {
            this.logHelper = serviceProvider.GetService(typeof(LogHelper)) as LogHelper;
            this.mapper = serviceProvider.GetService(typeof(IMapper)) as IMapper;
            this.platManager = serviceProvider.GetService(typeof(IUser)) as PlatManager;
            this.httpContextAccessor = serviceProvider.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;


        }

        public BaseController()
        {

        }

        protected Task<Result<T>> Success<T>(T data, string msg = null)
        {
            var result = new Result<T>()
            {
                msg = msg ?? "请求成功",
                success = true,
                data = data,
                status = 200
            };
            return Task.FromResult<Result<T>>(result);
        }
        protected Task<Result<T>> Fail<T>(string msg = null)
        {
            var result = new Result<T>()
            {
                msg = msg ?? "请求失败",
                success = false,
                status = 500
            };
            return Task.FromResult<Result<T>>(result);
        }

        protected Task<Result> Success(string msg = null)
        {
            var result = new Result()
            {
                msg = msg ?? "请求成功",
                success = true,
                status = 200
            };
            return Task.FromResult<Result>(result);
        }
        protected Task<Result> Fail(string msg = null)
        {
            var result = new Result()
            {
                msg = msg ?? "请求失败",
                success = false,
                status = 500
            };
            return Task.FromResult<Result>(result);
        }

        protected Task<Result<T>> Token_Expired<T>(string msg = null)
        {
            var result = new Result<T>()
            {
                msg = msg ?? "token已过期，请重新登陆",
                success = false,
                status = 401
            };
            return Task.FromResult<Result<T>>(result);
        }

        protected string GetBodyText(Encoding encoding = null)
        {
            var postJson = "";
            var stream = Request.Body;
            stream.Position = 0;
            long? length = Request.ContentLength;
            if (length != null && length > 0)
            {
                // 使用这个方式读取，并且使用异步
                if (encoding == null) encoding = Encoding.UTF8;
                StreamReader streamReader = new StreamReader(stream, encoding);
                postJson = streamReader.ReadToEndAsync().Result;
            }
            Request.HttpContext.Request.Body.Position = 0;
            return postJson;
        }



    }
}
